package org.rsbot.script.randoms;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.logging.Logger;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"illusion", "Pwnaz0r"}, name="Pillory", version=3.8D)
public class Pillory extends Random
  implements MessageListener
{
  public int fail = 0;
  private final int GameInterface = 189;
  private String pilloryMessage = "Solve the Pillory";
  public boolean inCage = false;
  public RSTile myLoc;
  public RSTile South = new RSTile(2606, 3105);
  RSTile[] cagetiles = { new RSTile(2608, 3105), new RSTile(2606, 3105), new RSTile(2604, 3105), new RSTile(3226, 3407), new RSTile(3228, 3407), new RSTile(3230, 3407), new RSTile(2685, 3489), new RSTile(2683, 3489), new RSTile(2681, 3489) };

  public boolean activateCondition()
  {
    if (!this.game.isLoggedIn()) {
      return false;
    }
    for (RSTile localRSTile : this.cagetiles) {
      if (getMyPlayer().getLocation().equals(localRSTile)) {
        return true;
      }
    }
    if (!this.inCage) {
      this.inCage = this.interfaces.getComponent(372, 3).getText().contains("Solve the pillory");
    }

    if (!this.inCage) {
      this.inCage = this.interfaces.getComponent(372, 3).getText().contains("swinging");
    }

    return this.inCage;
  }

  public RSObject findMYObject(int[] paramArrayOfInt)
  {
    Object localObject = null;
    int i = -1;
    for (int j = 0; j < 104; j++) {
      for (int k = 0; k < 104; k++) {
        RSObject[] arrayOfRSObject = this.objects.getAllAt(new RSTile(j + this.game.getBaseX(), k + this.game.getBaseY()));
        if (arrayOfRSObject.length > 0) {
          RSObject localRSObject = arrayOfRSObject[0];
          int m = 0;
          for (int i3 : paramArrayOfInt) {
            if (localRSObject.getID() == i3) {
              m = 1;
              break;
            }
          }
          if (m != 0) {
            int n = this.calc.distanceTo(localRSObject.getLocation());
            if (n != -1) {
              if (localObject == null) {
                i = n;
                localObject = localRSObject;
              } else if (n < i) {
                localObject = localRSObject;
                i = n;
              }
            }
          }
        }
      }
    }
    return localObject;
  }

  private int getKey() {
    int i = 0;
    this.log.info("\tKey needed :");
    switch (this.interfaces.get(189).getComponent(4).getModelID()) {
    case 9753:
      i = 9749;
      this.log.info("\t   Diamond");
      break;
    case 9754:
      i = 9750;
      this.log.info("\t   Square");
      break;
    case 9755:
      i = 9751;
      this.log.info("\t   Circle");
      break;
    case 9756:
      i = 9752;
      this.log.info("\t   Triangle");
    }

    if (this.interfaces.get(189).getComponent(5).getModelID() == i)
      return 1;
    if (this.interfaces.get(189).getComponent(6).getModelID() == i)
      return 2;
    if (this.interfaces.get(189).getComponent(7).getModelID() == i)
      return 3;
    return -1;
  }

  public int loop()
  {
    this.camera.setPitch(true);
    if (this.calc.distanceTo(this.South) <= 10)
      this.camera.setAngle(180);
    else {
      this.camera.setAngle(360);
    }
    if (this.fail > 20) {
      stopScript(false);
    }
    if (this.myLoc == null) {
      this.myLoc = getMyPlayer().getLocation();
      return random(1000, 2000);
    }
    if (!getMyPlayer().getLocation().equals(this.myLoc)) {
      this.log.info("Solved It.");
      this.myLoc = null;
      this.inCage = false;
      return -1;
    }
    if (!this.interfaces.get(189).isValid()) {
      Point localPoint1 = new Point(this.calc.tileToScreen(this.myLoc));
      Point localPoint2 = new Point((int)localPoint1.getX() + 10, (int)localPoint1.getY() - 30);
      this.mouse.click(localPoint2.x, localPoint2.y + random(0, 15), false);
      sleep(random(600, 800));
      if (this.menu.doAction("unlock")) {
        this.log.info("Successfully opened the lock!");
        return random(1000, 2000);
      }
      this.fail += 1;
    }

    if (this.interfaces.get(189).isValid()) {
      int i = getKey();
      this.log.info(String.valueOf(i));
      switch (i) {
      case 1:
        this.mouse.click(this.interfaces.get(189).getComponent(5).getArea().getLocation().x + random(10, 13), this.interfaces.get(189).getComponent(5).getArea().getLocation().y + random(46, 65), true);
        break;
      case 2:
        this.mouse.click(this.interfaces.get(189).getComponent(6).getArea().getLocation().x + random(10, 13), this.interfaces.get(189).getComponent(6).getArea().getLocation().y + random(46, 65), true);
        break;
      case 3:
        this.mouse.click(this.interfaces.get(189).getComponent(7).getArea().getLocation().x + random(10, 13), this.interfaces.get(189).getComponent(7).getArea().getLocation().y + random(46, 65), true);
        break;
      default:
        this.log.info("Bad Combo?");
        this.fail += 1;
      }

      return random(1000, 1600);
    }
    return -1;
  }

  public void messageReceived(MessageEvent paramMessageEvent) {
    String str = paramMessageEvent.getMessage();
    if ((str != null) && (str.contains(this.pilloryMessage)))
      this.inCage = true;
  }
}